package com.army.qinhongjun.slice;

import com.army.qinhongjun.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.PixelMapHolder;
import ohos.global.resource.NotExistException;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;

import java.io.IOException;
import java.io.InputStream;

public class MainAbilitySlice extends AbilitySlice {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001101, "MainAbilitySlice");

    Image image;
    PixelMap pixelMap;
    Button whirlImageBtn;
    Button cropImageBtn;
    Button scaleImageBtn;
    Button mirrorImageBtn;

    private int whirlCount = 0;
    private boolean isCorp = false;
    private boolean isScale = false;
    private boolean isMirror = false;
    private float scaleX = 1.0f;

    //默认按钮背景色
    ShapeElement normal = new ShapeElement();
    //激活按钮背景色
    ShapeElement active = new ShapeElement();

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        //初始化视图
        initView();
    }

    private void initView() {
        //从资源文件组件ID,获取相应组件
        whirlImageBtn = (Button)findComponentById(ResourceTable.Id_whirl_image);
        cropImageBtn = (Button)findComponentById(ResourceTable.Id_crop_image);
        scaleImageBtn = (Button)findComponentById(ResourceTable.Id_scale_image);
        mirrorImageBtn = (Button)findComponentById(ResourceTable.Id_mirror_image);
        image = (Image)findComponentById(ResourceTable.Id_image);
        //给相应按钮设置点击监听
        whirlImageBtn.setClickedListener(new ButtonClick());
        cropImageBtn.setClickedListener(new ButtonClick());
        scaleImageBtn.setClickedListener(new ButtonClick());
        mirrorImageBtn.setClickedListener(new ButtonClick());

        //默认按钮背景色
        normal.setRgbColor(new RgbColor(233, 219, 141));
        normal.setCornerRadius(40);
        //激活按钮背景色
        active.setRgbColor(new RgbColor(255, 0, 0));
        active.setCornerRadius(40);
    }

    private class ButtonClick implements Component.ClickedListener {
        @Override
        public void onClick(Component component) {

            int btnId = component.getId();
            switch (btnId) {
                case ResourceTable.Id_whirl_image:
                    // 图片旋转
                    whirlCount++;
                    isCorp = false;
                    isScale = false;
                    isMirror = false;
                    pixelMap = getPixelMapFromResource(ResourceTable.Media_1024);
                    image.setPixelMap(pixelMap);
                    whirlImageBtn.setBackground(active);
                    cropImageBtn.setBackground(normal);
                    scaleImageBtn.setBackground(normal);
                    mirrorImageBtn.setBackground(normal);
                    break;
                case ResourceTable.Id_crop_image:
                    // 图片剪裁
                    whirlCount = 0;
                    isCorp = !isCorp;
                    isScale = false;
                    isMirror = false;
                    pixelMap = getPixelMapFromResource(ResourceTable.Media_1024);
                    image.setPixelMap(pixelMap);
                    whirlImageBtn.setBackground(normal);
                    cropImageBtn.setBackground(active);
                    scaleImageBtn.setBackground(normal);
                    mirrorImageBtn.setBackground(normal);
                    break;
                case ResourceTable.Id_scale_image:
                    // 图片缩放
                    whirlCount = 0;
                    isCorp = false;
                    isScale = !isScale;
                    isMirror = false;
                    pixelMap = getPixelMapFromResource(ResourceTable.Media_1024);
                    image.setPixelMap(pixelMap);
                    whirlImageBtn.setBackground(normal);
                    cropImageBtn.setBackground(normal);
                    scaleImageBtn.setBackground(active);
                    mirrorImageBtn.setBackground(normal);
                    break;
                case ResourceTable.Id_mirror_image:
                    // 图片镜像
                    whirlCount = 0;
                    isCorp = false;
                    isScale = false;
                    isMirror = true;
                    pixelMap = getPixelMapFromResource(ResourceTable.Media_1024);
                    mirrorImage(pixelMap);
                    image.setPixelMap(pixelMap);
                    whirlImageBtn.setBackground(normal);
                    cropImageBtn.setBackground(normal);
                    scaleImageBtn.setBackground(normal);
                    mirrorImageBtn.setBackground(active);
                    break;
                default:
                    break;
            }
        }
    }

    /**
     * 镜像图片
     * @param pixelMap
     */
    private void mirrorImage(PixelMap pixelMap) {
        scaleX = -scaleX;
        image.addDrawTask(new Component.DrawTask() {
            @Override
            public void onDraw(Component component, Canvas canvas) {
                if (isMirror) {
                    isMirror = false;
                    PixelMapHolder pmh =new PixelMapHolder(pixelMap);
                    canvas.scale(scaleX, 1.0f,
                            (float)pixelMap.getImageInfo().size.width / 2,
                            (float)pixelMap.getImageInfo().size.height / 2);

                    canvas.drawPixelMapHolder(pmh, 0, 0, new Paint());
                }
            }
        });
    }

    /**
     * 根据图片ID获取PixelMap
     * @param resourceId
     * @return
     */
    private PixelMap getPixelMapFromResource(int resourceId) {
        InputStream inputStream = null;
        try {
            inputStream = getContext().getResourceManager().getResource(resourceId);
            //创建图片数据源ImageSource对象
            ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions();
            srcOpts.formatHint = "image/jpg";
            ImageSource imageSource = ImageSource.create(inputStream, srcOpts);

            //设置图片参数
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            //旋转设置
            decodingOptions.rotateDegrees = 90 * whirlCount;
            //缩放设置
            decodingOptions.desiredSize = new Size(isScale ? 512:0, isScale ? 384:0);
            //剪裁设置
            decodingOptions.desiredRegion = new Rect(0, 0, isCorp ? 1024:0, isCorp ? 400:0);
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;

            return imageSource.createPixelmap(decodingOptions);
        } catch (IOException e) {
            HiLog.error(LABEL_LOG, "IOException");
        } catch (NotExistException e) {
            HiLog.error(LABEL_LOG, "NotExistException");
        } finally {
            if (null != inputStream) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    HiLog.error(LABEL_LOG, "inputStream IOException");
                }
            }
        }
        return null;
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
